package com.example.canvastouch2;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.SeekBar;

public class WidthDialog extends Dialog {

    public interface OnWidthChangedListener {
        void widthChanged(int width);
    }
    private OnWidthChangedListener mListener;

	private int width = 0;
	private WidthView wv;
	
	public WidthDialog(	Context context,
						OnWidthChangedListener listener, 
						int width) {
		super(context);
		this.mListener = listener;
		this.width = width;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setTitle("線の幅を設定してください。");
		this.setContentView(R.layout.content_width);
		
		wv = (WidthView)this.findViewById(R.id.widthView);
		wv.setWidth(width);
		
		SeekBar bar = (SeekBar)this.findViewById(R.id.seekBar_width);
		bar.setMax(50);
		bar.setProgress(this.width);
		bar.setOnSeekBarChangeListener(new SeekBarListener());
	}
	
	public class SeekBarListener implements SeekBar.OnSeekBarChangeListener {

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			if (fromUser) {
				showWidth(progress);
			}
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			showWidth(seekBar.getProgress());
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			mListener.widthChanged(seekBar.getProgress());
			dismiss();
		}
		
		private void showWidth(int width) {
			wv.setWidth(width);
			wv.invalidate();
		}
		
	}

}
